<?php

namespace App\Http\Models\Admin;

use App\Observers\BosCleanObserver;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;

/**
 * @property int id
 * @property int product_category_id
 * @property string|null information - 资料下载 key / 地址
 * @property string specification - 规格
 * @property string detail - 详情 html
 * @property string|null title - 标题
 * @property string|null model_number - 型号
 * @property string|null keywords - SEO关键字
 * @property string|null description - 描述
 *
 * @property ProductCategory $productCategory - 所属分类
 *
 * @method static self filterProductCategoryId(int $product_category_id);
 * @method static self filterModelNumber(string $model_number);
 * @method static self filterTitle(string $title);
 * @method static self filterKeyWord(string $key_word);
 */

class Product extends Model
{
    use Bosable;

    public $bos_columns = ['thumb', 'information'];

    protected $thumb_bos_key_prefix = 'products/thumb/';   // 产品缩略图路径前缀

    protected $information_bos_key_prefix = 'products/information/'; // 产品资料路径前缀

    protected $table = 'products';

    protected $guarded = [];

    protected $casts = [
        'product_category_id' => 'int'
    ];

    /**
     * @used-by $productCategory
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function productCategory()
    {
        return $this->belongsTo(ProductCategory::class, 'product_category_id');
    }
    
    /**
     * @param UploadedFile|null $information
     */
    public function setInformationAttribute($information)
    {
        if (is_file($information)) {
            $this->attributes['information'] = $this->uploadPostFile($information , $this->information_bos_key_prefix);
        } else if (is_null($information)) {
            $this->attributes['information'] = null;
        }
    }

    /**
     * @return null|string
     */
    public function getInformationAttribute()
    {
        if (! $this->attributes['information']) return null;

        return $this->getStaticUrl($this->attributes['information']);
    }

    /**
     * @param $thumb
     */
    public function setThumbAttribute($thumb)
    {
        if (is_file($thumb)) {
            $this->attributes['thumb'] = $this->uploadPostFile($thumb , $this->thumb_bos_key_prefix);
        }
    }

    /**
     * @return null|string
     */
    public function getThumbAttribute()
    {
        if (! $this->attributes['thumb']) return null;

        return $this->getStaticUrl($this->attributes['thumb']);
    }

    /**
     * @param Builder $query
     * @param $product_category_id
     * @return Builder
     */
    public function scopeFilterProductCategoryId(Builder $query, $product_category_id)
    {
        if (!$product_category_id) return $query;

        return $query->where('product_category_id', $product_category_id);
    }

    /**
     * @param Builder $query
     * @param $model_number
     * @return Builder
     */
    public function scopeFilterModelNumber(Builder $query, $model_number)
    {
        if (!$model_number) return $query;

        return $query->where('model_number', 'like', '%'.$model_number.'%');
    }

    /**
     * @param Builder $query
     * @param $title
     * @return Builder
     */
    public function scopeFilterTitle(Builder $query, $title)
    {
        if (!$title) return $query;

        return $query->where('title', 'like', '%'.$title.'%')
            ->orWhere('sub_title', 'like', '%'.$title.'%');
    }

    /**
     * @param Builder $query
     * @param $key_word
     * @return Builder
     */
    public function scopeFilterKeyWord(Builder $query, $key_word)
    {
        if (!$key_word) return $query;

        return $query->where('title', 'like', '%'.$key_word.'%')
            ->orWhere('model_number', 'like', '%'.$key_word.'%');
    }

    /**
     *  删除产品资料的信息
     *
     * @return bool
     */
    public function deleteInformation()
    {
        $deleted = $this->deleteBosKey($this->attributes['information']);

        return $deleted;
    }

    /**
     *  注册删除bosKey事件绑定
     *
     */
    protected static function boot()
    {
        parent::boot();

        /*
         * 监听bos清理事件
         */
        self::observe((BosCleanObserver::class));
    }
}
